﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Windows;

namespace AGameOfThrones.game
{
    public class piece : INotifyPropertyChanged, stream.streamable
    {

        public game Game
        {
            get
            {
                try
                {
                    return Application.Current.FindResource("gamestate") as game;
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex);
                    return null;
                }

            }
        }

        public void Initialize()
        {

            if (PieceName != "")
            {
                if (Game.Pieces.ContainsKey(PieceName))
                {
                    Game.Pieces[PieceName] = this;
                }
                else
                {
                    Game.Pieces.Add(PieceName, this);
                }
            }
        }

        private position _position = new position();
        public position Position
        {
            get { return _position; }
            set
            {
                if (_position != value)
                {
                    _position = value;
                    Notify("Position");
                }
            }
        }
        
        public String piecename = "";
        public String PieceName
        {
            get { return piecename; }
            set
            {
                if (piecename != value)
                {
                    piecename = value;

                    Notify("PieceName");
                }
            }
        }
                
        public event PropertyChangedEventHandler PropertyChanged;
        protected void Notify(string property)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));  
            }
        }

        public virtual void serialize(stream.stream io_Stream)
        {
            stream.streamable s = _position;

            io_Stream.begin("Piece")
                .serialize("PieceName", ref piecename)
                .serialize("Position", ref s)
            .end();
        }
    }
}
